package cc.fs.zhihuapp.rest.asynchttp;


import android.util.Log;

import com.google.gson.Gson;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import cc.fs.zhihuapp.model.Category;
import cc.fs.zhihuapp.model.DayNews;
import cc.fs.zhihuapp.model.NewsHtml;
import cc.fs.zhihuapp.model.VideoBean;
import cc.fs.zhihuapp.utils.MyCallback;

/**
 * Created by fostion on 2015/9/9.
 * 使用gson解析可以快速解析成object
 */
public class VideoApi {

    public static Gson gson = new Gson();
    public static String HOST = "http://192.168.1.101";
    public static String PORT = "8888";
    public static String ADDRESS = HOST + ":" + PORT;
    private static String GET_VIDEOS = ADDRESS+"/video";

    public static void setHost(String host){
        HOST = host;
    }

    public static void getVideos(final MyCallback.NetworkCallback0<ArrayList<VideoBean>> callback){
        ApiHelper.get(GET_VIDEOS , new ApiHandler() {
            @Override
            public void onSuccess(JSONObject object) {
                Log.e("success ",object.toString());
                ArrayList<VideoBean> videos = new ArrayList<VideoBean>();
                try {
                    JSONArray videoArray = object.getJSONArray("data");
                    for(int i=0;i<videoArray.length();i++){
                        VideoBean video = gson.fromJson(videoArray.get(i).toString(), VideoBean.class);
                        videos.add(video);
                    }
                } catch (JSONException e) {
                   callback.failure("json errror");
                }

                callback.success(videos);
            }

            @Override
            public void onFailure(String error) {
                Log.e("error","----");
                if(error != null)
                   callback.failure(error);
                else
                    callback.failure("unknow error");
            }
        });
    }

}
